package com.sertaogames.mundoj.model;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Text;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

@PersistenceCapable
public class Compra implements Serializable{

	private static final long serialVersionUID = 1L;

	@PrimaryKey
	@Persistent(valueStrategy=IdGeneratorStrategy.IDENTITY)
	private Long id;

	@NotPersistent
	private List<ItemCompra> itens = new ArrayList<ItemCompra>();

	private Text itensJSON;

	private Date data = new Date();

	private BigDecimal total = BigDecimal.ZERO;

	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}

	public Date getData() {
		return data;
	}
	public void setData(Date data) {
		this.data = data;
	}

	public List<ItemCompra> getItens() {
		return itens;
	}
	public void setItens(List<ItemCompra> itens) {
		this.itens = itens;
	}

	public Text getItensJSON() {
		return itensJSON;
	}
	public void setItensJSON(Text itensJSON) {
		this.itensJSON = itensJSON;
	}

	public BigDecimal getTotal() {
		return total;
	}
	public void setTotal(BigDecimal total) {
		this.total = total;
	}

	public void saveJSON() {
		Gson gson = new Gson();
		this.itensJSON = new Text(gson.toJson(this.itens));
	}

	public void loadJSON() {
		Gson gson = new Gson();
		this.itens = gson.fromJson(this.itensJSON.getValue(), new TypeToken<List<ItemCompra>>(){}.getType());
	}


}
